home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue32 / kronos / KRONOS.ZIP / Main.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-02-22  |  20.4 KB  |  737 lines

  1. unit Main;
  2. {
  3. This program demonstrates some of the key features of TKronos:
  4.  
  5. ***
  6. It loads a daytype definition file containg data about authors and
  7. display their birth and deaths in intervals of 25 years.
  8.  
  9. The daytype definition file also caontains a user calculated daytype
  10. 'My calcday' that is programmed to show up every last friday in a month.
  11. If that friday happens to be a holiday, the first none holiday prior to
  12. last friday is selected.
  13.  
  14. ***
  15.  
  16. It implements basic functionality for navgigating a calendar.
  17.  
  18. ***
  19.  
  20. It implements an interface for the user to edit, delete and add daytypes -
  21. and to load and save daytype definitions to file.
  22. }
  23.  
  24.  
  25. interface
  26.  
  27. uses
  28.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  29.   Kronos, StdCtrls, Grids, Mask, ComCtrls, ExtCtrls;
  30.  
  31. type
  32.  
  33.   TForm1 = class(TForm)
  34.     DrawGrid1: TDrawGrid;
  35.     LabelMonthName: TLabel;
  36.     LabelYear: TLabel;
  37.     ButtonNextYear: TButton;
  38.     ButtonPrevYear: TButton;
  39.     ButtonNextMonth: TButton;
  40.     ButtonPrevMonth: TButton;
  41.     ButtonNextWeek: TButton;
  42.     ButtonPrevWeek: TButton;
  43.     ButtonToday: TButton;
  44.     ButtonGo: TButton;
  45.     ButtonTomorrow: TButton;
  46.     ButtonYesterday: TButton;
  47.     ButtonThisweek: TButton;
  48.     ButtonThisMonth: TButton;
  49.     ButtonNextM: TButton;
  50.     ButtonLastMonth: TButton;
  51.     ButtonNextW: TButton;
  52.     ButtonLastWeek: TButton;
  53.     EditYear: TEdit;
  54.     ListBoxYE: TListBox;
  55.     Label2: TLabel;
  56.     ButtonAdd: TButton;
  57.     ButtonDelete: TButton;
  58.     ButtonSave: TButton;
  59.     ButtonLoad: TButton;
  60.     Label1: TLabel;
  61.     Label3: TLabel;
  62.     Label4: TLabel;
  63.     Label5: TLabel;
  64.     Label6: TLabel;
  65.     EditMonth: TEdit;
  66.     EditMonthday: TEdit;
  67.     Label7: TLabel;
  68.     Label8: TLabel;
  69.     Label9: TLabel;
  70.     Label10: TLabel;
  71.     Label11: TLabel;
  72.     EditWeek: TEdit;
  73.     ComboBoxWeekday: TComboBox;
  74.     Label12: TLabel;
  75.     ButtonEdit: TButton;
  76.     OpenDialog1: TOpenDialog;
  77.     SaveDialog1: TSaveDialog;
  78.     ComboBoxFirstWd: TComboBox;
  79.     Label13: TLabel;
  80.     Kronos1: TKronos;
  81.     RGEvents: TRadioGroup;
  82.     procedure FormCreate(Sender: TObject);
  83.     procedure DrawGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  84.       Rect: TRect; State: TGridDrawState);
  85.     procedure Kronos1ChangeYear(Sender: TObject);
  86.     procedure Kronos1ChangeMonthNumber(Sender: TObject);
  87.     procedure ButtonNextYearClick(Sender: TObject);
  88.     procedure ButtonPrevYearClick(Sender: TObject);
  89.     procedure ButtonNextMonthClick(Sender: TObject);
  90.     procedure ButtonPrevMonthClick(Sender: TObject);
  91.     procedure ButtonTodayClick(Sender: TObject);
  92.     procedure DrawGrid1SelectCell(Sender: TObject; Col, Row: Integer;
  93.       var CanSelect: Boolean);
  94.     procedure Kronos1ChangeMonth(Sender: TObject);
  95.     procedure Kronos1ChangeMonthDay(Sender: TObject);
  96.     procedure ButtonNextWeekClick(Sender: TObject);
  97.     procedure ButtonPrevWeekClick(Sender: TObject);
  98.     procedure ButtonGoClick(Sender: TObject);
  99.     procedure ButtonTomorrowClick(Sender: TObject);
  100.     procedure ButtonYesterdayClick(Sender: TObject);
  101.     procedure ButtonThisweekClick(Sender: TObject);
  102.     procedure ButtonThisMonthClick(Sender: TObject);
  103.     procedure ButtonNextMClick(Sender: TObject);
  104.     procedure ButtonLastMonthClick(Sender: TObject);
  105.     procedure ButtonNextWClick(Sender: TObject);
  106.     procedure ButtonLastWeekClick(Sender: TObject);
  107.     procedure ListBoxYEDblClick(Sender: TObject);
  108.     procedure ComboBoxWeekdayKeyDown(Sender: TObject; var Key: Word;
  109.       Shift: TShiftState);
  110.     procedure ButtonAddClick(Sender: TObject);
  111.     procedure ButtonDeleteClick(Sender: TObject);
  112.     procedure ButtonEditClick(Sender: TObject);
  113.     procedure ButtonSaveClick(Sender: TObject);
  114.     procedure ButtonLoadClick(Sender: TObject);
  115.     procedure ComboBoxFirstWdChange(Sender: TObject);
  116.     procedure RGEventsClick(Sender: TObject);
  117.     procedure Kronos1ChangeDate(Sender: TObject);
  118.     procedure Kronos1CalcDaytype(Sender: TObject; Daytype: TDaytype;
  119.       ADateExt: TDateExt; IsCurrentDate: Boolean; var Accept: Boolean);
  120.   private
  121.     { Private declarations }
  122.     SelCol, SelRow : Longint;
  123.     CalcDay : Integer;
  124.     procedure ListEvents;
  125.   public
  126.     { Public declarations }
  127.     UserDay : TDaytypeDef;
  128.   end;
  129.  
  130. var
  131.   Form1: TForm1;
  132.  
  133. implementation
  134. uses Daytype;
  135.  
  136. {$R *.DFM}
  137.  
  138. procedure InvalidateCell(Grid : TDrawGrid; C, R : integer;
  139. Erase : boolean);
  140. var
  141.    Rect : Trect;
  142. begin
  143.      with Grid do
  144.      begin
  145.           Rect := CellRect(C, R);
  146.           InvalidateRect(Handle, @Rect, Erase);
  147.      end;
  148. end;
  149.  
  150. procedure TForm1.FormCreate(Sender: TObject);
  151. begin
  152.      with Kronos1 do
  153.      begin
  154.           GetMIDayCell(DayNumber, SelRow, SelCol);
  155.           ComboBoxFirstWd.ItemIndex := Ord(FirstWeekday);
  156.           LoadFromFile('authors.kdt', true);
  157.      end;
  158.      with DrawGrid1 do
  159.      begin
  160.           ColWidths[0] := 40;
  161.           Rowheights[0] := 20;
  162.      end;
  163.      Kronos1.ReChange;
  164. end;
  165.  
  166. procedure TForm1.ListEvents;
  167. // Fill the Year events list
  168. var
  169.    I, J : integer;
  170.    DateInf : TDateExt;
  171.    Daytype : TDaytype;
  172.    S : string;
  173. begin
  174.      ListBoxYe.Clear;
  175.  
  176.      if RGEvents.ItemIndex = 1 then
  177.      with Kronos1 do
  178.      begin
  179.           DateInf := FetchDateExtDn(Year, Daynumber);
  180.           for J := 1 to DateInf.DaytypeCount do
  181.           begin
  182.                Daytype := FetchDaytype(DateInf, J);
  183.                S := Daytype.TheName;
  184.                ListboxYe.Items.AddObject(S, Daytype);
  185.           end;
  186.           exit;
  187.      end;
  188.  
  189.      for I := 1 to Kronos1.YearExt.YearTypeCount do
  190.      begin
  191.           with Kronos1 do
  192.           begin
  193.                DayType := FetchYeartype(YearExt, I);
  194.                S := IntToStr(Year - Daytype.FirstShowUp) + ' ' +
  195.                'years since ' + Daytype.TheName;
  196.           end;
  197.           ListboxYe.Items.AddObject(S, Daytype);
  198.      end;
  199. end;
  200.  
  201. procedure TForm1.DrawGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  202.   Rect: TRect; State: TGridDrawState);
  203. {Most of the code here is concerned with drawing graphics. Calendaric
  204. data is very simply extracted from the Kronos Component}
  205.  
  206.    procedure WriteDayType(DayInf : TDateExt);
  207.    //Extracting daytypes connected to a date
  208.    var
  209.       i : integer;
  210.       OldSize : integer;
  211.       OldName : TFontName;
  212.       DT : TDayType;
  213.       FirstWord, SecondWord : string;
  214.       FirstWordIndex : integer;
  215.       S : string;
  216.    begin
  217.         with DrawGrid1.Canvas do
  218.         begin
  219.              OldSize := Font.Size;
  220.              OldName := Font.Name;
  221.              Font.Name := 'Arial';
  222.              Font.Size := 7;
  223.         end;
  224.  
  225.         with Kronos1 do
  226.         begin
  227.              for i := 1 to DayInf.DaytypeCount do
  228.              begin
  229.                   DT := FetchDaytype(DayInf,1);
  230.                   S := DT.TheName;
  231.                   FirstWordIndex := Pos(' ', S);
  232.                   if FirstWordIndex <> 0 then
  233.                   begin
  234.                      FirstWord := copy(S, 1, FirstWordIndex - 1);
  235.                      SecondWord := copy(S, FirstWordIndex + 1,
  236.                      Length(S) - FirstWordIndex);
  237.                   end
  238.                   else
  239.                   begin
  240.                      FirstWord := '';
  241.                      SecondWord := Dt.TheName;
  242.                   end;
  243.                   if DrawGrid1.Canvas.TextWidth(S) >
  244.                   (DrawGrid1.DefaultColWidth - 25) then
  245.                   begin
  246.                        DrawGrid1.Canvas.TextOut(Rect.Left + 22, Rect.Top + 3,
  247.                        FirstWord);
  248.                        DrawGrid1.Canvas.TextOut(Rect.Left + 22, Rect.Top + 15,
  249.                        SecondWord);
  250.                   end
  251.                   else
  252.                       DrawGrid1.Canvas.TextOut(Rect.Left + 22, Rect.Top + 3,
  253.                       S);
  254.                   break;
  255.              end;
  256.              {Extract and print the first daytype, if any}
  257.         end;
  258.         DrawGrid1.Canvas.Font.Size := OldSize;
  259.         DrawGrid1.Canvas.Font.Name := OldName;
  260.    end;
  261.  
  262.    procedure WriteWeekCaption;
  263.    // Write 'Week' in upper left corner
  264.    begin
  265.           with DrawGrid1.Canvas do
  266.           begin
  267.                Brush.Color := clAqua;
  268.                Font.Color := clBlue;
  269.                FillRect(Rect);
  270.                TextOut(Rect.Left+5,Rect.Top+2,'Week');
  271.           end;
  272.    end;
  273.  
  274.    procedure WriteDayname;
  275.    // Write dayname in first row
  276.    var
  277.       NameIndex : word;
  278.       DName : string;
  279.    begin
  280.           NameIndex := Kronos1.DOWtoDayNameIndex(Col);
  281.           // Get index to use with the Daynames-array
  282.           DName := Kronos1.DayNames[NameIndex];
  283.           DName[1] := Upcase(DName[1]);
  284.           with DrawGrid1.Canvas do
  285.           begin
  286.                Brush.Color := clAqua;
  287.                Font.Color := clBlue;
  288.                FillRect(Rect);
  289.           end;
  290.           DrawGrid1.Canvas.TextOut(Rect.Left+5,Rect.Top+2,Dname);
  291.    end;
  292.  
  293.    procedure WriteWeeknumber;
  294.    var
  295.       WeekNo : string;
  296.    begin
  297.           WeekNo := IntToStr
  298.           (Abs(Kronos1.MonthExt.MonthImage[Row,Col]));
  299.           //Weeknumber is read directly from the month image structure
  300.           with DrawGrid1.Canvas do
  301.           begin
  302.                Brush.Color := clAqua;
  303.                Font.Color := clBlack;
  304.                FillRect(Rect);
  305.           end;
  306.           DrawGrid1.Canvas.TextOut(Rect.Left+5,Rect.Top+2,WeekNo);
  307.    end;
  308.  
  309.    procedure WriteMonthday;
  310.    var
  311.       MonthD : string;
  312.       BoundMonth : boolean;
  313.       Dnr : smallint;
  314.       DayInf : TDateExt;
  315.    begin
  316.           BoundMonth := false;
  317.           with Kronos1 do
  318.           begin
  319.                MonthD := '';
  320.                Dnr := MonthExt.MonthImage[Row,Col];
  321.                if Dnr < 0 then // Date from bounding months has neg. numbers
  322.                begin
  323.                     MonthD := IntToStr(Abs(Dnr));
  324.                     BoundMonth := true;
  325.                end
  326.                else if Dnr > 0 then
  327.                begin // Daynumber from actual month
  328.                     DayInf := FetchDateExtDn(Year, Dnr);
  329.                     {Get DateExt using the daynumber i the month image cell.
  330.                     The rows and cols in the grid corresponds to the
  331.                     rows and cols in the month image structure}
  332.                     MonthD := IntToStr(DayInf.MonthDay);
  333.                end;
  334.                if (Col = SelCol) and (Row = SelRow) then
  335.                with DrawGrid1.Canvas do
  336.                begin
  337.                      Brush.Color := clBlue;
  338.                      Font.Color := clWhite;
  339.                end
  340.                else if BoundMonth then
  341.                with DrawGrid1.Canvas do
  342.                // Paint days from bounding months in gray
  343.                begin
  344.                     Brush.Color := clSilver;
  345.                     Font.Color := clGray;
  346.                end
  347.                else if DayInf.Holiday then
  348.                with DrawGrid1.Canvas do
  349.                // Paint holidays red
  350.                begin
  351.                     Brush.Color := clSilver;
  352.                     Font.Color := clMaroon;
  353.                end
  354.                else with DrawGrid1.Canvas do
  355.                // Paint normal days blue
  356.                begin
  357.                     Brush.Color := clSilver;
  358.                     Font.Color := clBlue;
  359.                end;
  360.  
  361.                with DrawGrid1.Canvas do
  362.                begin
  363.                     Font.Style := [fsBold];
  364.                     FillRect(Rect);
  365.                     TextOut(Rect.Left+5,Rect.Top+2,MonthD);
  366.                     Font.Style := [];
  367.                end;
  368.  
  369.                // Write first daytype, if any
  370.                if DNr > 0 then
  371.                   if DayInf.DaytypeCount > 0 then
  372.                      WriteDayType(DayInf);
  373.           end;
  374.    end;
  375.  
  376. begin
  377.      if (Row = 0) and (Col = 0) then
  378.         WriteWeekCaption
  379.      else if (Row = 0) and (Col > 0) then
  380.         WriteDayname
  381.      else if (Row > 0) and (Col = 0) then
  382.         WriteWeeknumber
  383.      else if (Row > 0) and (Col > 0) then
  384.         WriteMonthday;
  385.  
  386.      //Draw monthday rectangle
  387.      with DrawGrid1.Canvas do
  388.      begin
  389.           Pen.Color := clBlack;
  390.           Pen.Width := 1;
  391.           Brush.Style := bsClear;
  392.           Rectangle(Rect.Left+1,Rect.Top+1, Rect.Right-1,
  393.           Rect.Bottom-1);
  394.           Brush.Style := bsSolid;
  395.      end;
  396. end;
  397.  
  398. procedure TForm1.ButtonNextYearClick(Sender: TObject);
  399. begin
  400.      with Kronos1 do
  401.      Year := Year + 1;
  402. end;
  403.  
  404. procedure TForm1.ButtonPrevYearClick(Sender: TObject);
  405. begin
  406.      with Kronos1 do
  407.      Year := Year - 1;
  408. end;
  409.  
  410. procedure TForm1.ButtonNextMonthClick(Sender: TObject);
  411. begin
  412.      with Kronos1 do
  413.           GotoOffsetMonth(1);
  414. end;
  415.  
  416. procedure TForm1.ButtonPrevMonthClick(Sender: TObject);
  417. begin
  418.      with Kronos1 do
  419.           GotoOffsetMonth(-1);
  420. end;
  421.  
  422. procedure TForm1.ButtonTodayClick(Sender: TObject);
  423. begin
  424.      Kronos1.GotoToday;
  425. end;
  426.  
  427. procedure TForm1.DrawGrid1SelectCell(Sender: TObject; Col, Row: Integer;
  428.   var CanSelect: Boolean);
  429. begin
  430.      with Kronos1 do
  431.      if (Row = 0) or (Col = 0)
  432.      or (Row > MonthExt.NumWeeks)
  433.      or (MonthExt.MonthImage[Row, Col] < 1) then
  434.      begin
  435.           CanSelect := false;
  436.           exit;
  437.      end;
  438.      with Kronos1 do
  439.        Daynumber := MonthExt.MonthImage[Row,Col];
  440. end;
  441.  
  442. procedure TForm1.ButtonNextWeekClick(Sender: TObject);
  443. begin
  444.      Kronos1.GotoOffsetWeek(1);
  445. end;
  446.  
  447. procedure TForm1.ButtonPrevWeekClick(Sender: TObject);
  448. begin
  449.      Kronos1.GotoOffsetWeek(-1);
  450. end;
  451.  
  452. procedure TForm1.ButtonGoClick(Sender: TObject);
  453. var
  454.    Y,M,Md,W : word;
  455.    Wd : TWeekday;
  456.    WdSet : boolean;
  457. begin
  458.      WdSet := true;
  459.      if EditYear.Text <> '' then
  460.         Y := StrToInt(EditYear.Text)
  461.      else
  462.         Y := 0;
  463.      if EditMonth.Text <> '' then
  464.         M := StrToInt(EditMonth.Text)
  465.      else
  466.         M := 0;
  467.      if EditWeek.Text <> '' then
  468.         W := StrToInt(EditWeek.Text)
  469.      else
  470.         W := 0;
  471.      if EditMonthday.Text <> '' then
  472.         Md := StrToInt(EditMonthday.Text)
  473.      else
  474.         Md := 0;
  475.      if ComboboxWeekDay.ItemIndex <> -1 then
  476.         Wd := TWeekday(ComboboxWeekDay.ItemIndex)
  477.      else
  478.         WdSet := false;
  479.  
  480.      with Kronos1 do
  481.      begin
  482.           BeginChange;
  483.           try
  484.              if Y <> 0 then
  485.                 Year := Y;
  486.              if M <> 0 then
  487.                 Month := M;
  488.              if W <> 0 then
  489.                 Week := W;
  490.              if Md <> 0 then
  491.                 Monthday := Md;
  492.              if WdSet then
  493.                 WeekDay := Wd;
  494.           finally
  495.              EndChange;
  496.           end;
  497.      end;
  498. end;
  499.  
  500. procedure TForm1.ButtonTomorrowClick(Sender: TObject);
  501. begin
  502.      Kronos1.GotoTomorrow;
  503. end;
  504.  
  505. procedure TForm1.ButtonYesterdayClick(Sender: TObject);
  506. begin
  507.      Kronos1.GoToYesterday;
  508. end;
  509.  
  510. procedure TForm1.ButtonThisweekClick(Sender: TObject);
  511. begin
  512.      Kronos1.GotoThisWeek;
  513. end;
  514.  
  515. procedure TForm1.ButtonThisMonthClick(Sender: TObject);
  516. begin
  517.      Kronos1.GotoThisMonth;
  518. end;
  519.  
  520. procedure TForm1.ButtonNextMClick(Sender: TObject);
  521. begin
  522.      Kronos1.GotoNextMonth;
  523. end;
  524.  
  525. procedure TForm1.ButtonLastMonthClick(Sender: TObject);
  526. begin
  527.      Kronos1.GotoLastMonth;
  528. end;
  529.  
  530. procedure TForm1.ButtonNextWClick(Sender: TObject);
  531. begin
  532.      Kronos1.GotoNextWeek;
  533. end;
  534.  
  535. procedure TForm1.ButtonLastWeekClick(Sender: TObject);
  536. begin
  537.      Kronos1.GotoLastWeek;
  538. end;
  539.  
  540. procedure TForm1.ListBoxYEDblClick(Sender: TObject);
  541. var
  542.    Daytypename : string;
  543.    Ind : integer;
  544. begin
  545.      Ind := 0;
  546.      with ListBoxYe do
  547.         Daytypename := Items[ItemIndex];
  548.      Ind := Pos('birth of',Daytypename);
  549.      if Ind <> 0 then
  550.         exit
  551.      else
  552.      begin
  553.          Ind := Pos('death of',Daytypename);
  554.          if Ind <> 0 then
  555.             exit;
  556.      end;
  557.      Kronos1.GotoDayType(Kronos1.Year,0,DayTypeName);
  558. end;
  559.  
  560. procedure TForm1.ComboBoxWeekdayKeyDown(Sender: TObject; var Key: Word;
  561.   Shift: TShiftState);
  562. begin
  563.      if Key = VK_Escape then
  564.         ComboBoxWeekday.ItemIndex := -1;
  565. end;
  566.  
  567. procedure TForm1.ButtonAddClick(Sender: TObject);
  568. begin
  569.      ListBoxYe.ItemIndex := -1;
  570.      Application.CreateForm(TDaytypeDlg, DayTypeDlg);
  571.      if DayTypeDlg.ShowModal = mrOk then
  572.      begin
  573.           Kronos1.AddDaytype(TDaytype.Create(Userday));
  574.           Kronos1.UpdateInfo;
  575.           DrawGrid1.Refresh;
  576.           ListEvents;
  577.      end;
  578.  
  579. end;
  580.  
  581. procedure TForm1.ButtonDeleteClick(Sender: TObject);
  582. var
  583.    DayType : TDaytype;
  584. begin
  585.      if ListBoxYe.ItemIndex = -1 then
  586.      begin
  587.           ShowMessage('No daytype selected');
  588.           exit;
  589.      end;
  590.      Daytype := TDayType(ListBoxYe.Items.Objects[ListboxYe.ItemIndex]);
  591.      if Daytype.Id < Kronos1.FirstUserId then
  592.      begin
  593.         ShowMessage('Daytype is not userdefined');
  594.      end
  595.      else
  596.      begin
  597.           Kronos1.DeleteUserDaytype(Daytype.ID,'');
  598.           DrawGrid1.Refresh;
  599.           ListEvents;
  600.      end;
  601. end;
  602.  
  603. procedure TForm1.ButtonEditClick(Sender: TObject);
  604. var
  605.    Daytype : TDaytype;
  606. begin
  607.      if ListBoxYe.ItemIndex = -1 then
  608.      begin
  609.           ShowMessage('No daytype selected');
  610.           exit;
  611.      end;
  612.  
  613.      Daytype := TDaytype(ListBoxYe.Items.Objects[ListboxYe.ItemIndex]);
  614.      if Daytype.Id < Kronos1.FirstUserId then
  615.      begin
  616.         ShowMessage('Daytype is not userdefined');
  617.      end;
  618.  
  619.      Userday := Kronos1.GetDaytypeDef(Daytype.Id,'');
  620.      Application.CreateForm(TDaytypeDlg, DayTypeDlg);
  621.      if DayTypeDlg.ShowModal = mrOk then
  622.      begin
  623.           Kronos1.UpdateDaytype(Daytype.ID,'',Userday);
  624.           Kronos1.UpdateInfo;
  625.           DrawGrid1.Refresh;
  626.           ListEvents;
  627.      end;
  628. end;
  629.  
  630. procedure TForm1.ButtonSaveClick(Sender: TObject);
  631. begin
  632.      if SaveDialog1.Execute then
  633.      begin
  634.           Kronos1.SaveToFile(Savedialog1.FileName);
  635.      end;
  636. end;
  637.  
  638. procedure TForm1.ButtonLoadClick(Sender: TObject);
  639. begin
  640.      If OpenDialog1.Execute then
  641.      begin
  642.           Kronos1.LoadfromFile(OpenDialog1.Filename, true);
  643.           Drawgrid1.Refresh;
  644.           ListEvents;
  645.      end;
  646. end;
  647.  
  648. procedure TForm1.ComboBoxFirstWdChange(Sender: TObject);
  649. var
  650.    R, C : integer;
  651. begin
  652.      Kronos1.FirstWeekDay := Tweekday(ComboBoxFirstWd.ItemIndex);
  653.      Kronos1.GetMIDayCell(Kronos1.Daynumber,R, C);
  654.      if (R <> SelRow) or (C <> SelCol) then
  655.      begin
  656.           SelRow := R;
  657.           SelCol := C;
  658.      end;
  659.      Drawgrid1.Refresh;
  660. end;
  661.  
  662. procedure TForm1.RGEventsClick(Sender: TObject);
  663. begin
  664.      ListEvents;
  665. end;
  666.  
  667. {************************** Kronos Event handling**************************}
  668.  
  669. procedure TForm1.Kronos1ChangeYear(Sender: TObject);
  670. {Change year caption when year changes}
  671. begin
  672.      LabelYear.Caption := IntToStr(Kronos1.Year);
  673.      ListEvents;
  674. end;
  675.  
  676. procedure TForm1.Kronos1ChangeMonthNumber(Sender: TObject);
  677. {Change month caption when month changes}
  678. begin
  679.      LabelMonthName.Caption := Kronos1.MonthExt.MonthName;
  680. end;
  681.  
  682. procedure TForm1.Kronos1ChangeMonth(Sender: TObject);
  683. var
  684.    R, C : Longint;
  685.    DExt : TDateExt;
  686. begin
  687.      Kronos1.GetMIDayCell(Kronos1.DayNumber, SelRow, SelCol);
  688.      //Find last Friday of month to use with the OnCalcDaytype
  689.      Kronos1.GetLastMIDayCell(R,C);
  690.      with Kronos1.MonthExt do
  691.      begin
  692.           CalcDay := MonthImage[R,C];
  693.           while Kronos1.DowToWeekDay(C) <> Friday do
  694.           begin
  695.                dec(CalcDay);
  696.                Kronos1.GetMIDayCell(CalcDay, R, C);
  697.           end;
  698.           // If last Friday is holiday, move backwards to first none holiday
  699.           DExt := Kronos1.FetchDateExtDn(Year, CalcDay);
  700.           while DExt.Holiday do
  701.           begin
  702.                dec(CalcDay);
  703.                DExt := Kronos1.FetchDateExtDn(Year,CalcDay);
  704.           end;
  705.      end;
  706.      DrawGrid1.Refresh;
  707. end;
  708.  
  709. procedure TForm1.Kronos1ChangeMonthDay(Sender: TObject);
  710. var
  711.    OldSelRow, OldSelCol : integer;
  712. begin
  713.      with Kronos1 do
  714.      begin
  715.           OldSelRow := SelRow;
  716.           OldSelCol := SelCol;
  717.           GetMIDayCell(DayNumber, SelRow, SelCol);
  718.           InvalidateCell(DrawGrid1, OldSelCol, OldSelRow, False);
  719.           InvalidateCell(DrawGrid1, SelCol, SelRow, False);
  720.      end;
  721. end;
  722.  
  723. procedure TForm1.Kronos1ChangeDate(Sender: TObject);
  724. begin
  725.      ListEvents;
  726. end;
  727.  
  728. procedure TForm1.Kronos1CalcDaytype(Sender: TObject; Daytype: TDaytype;
  729.   ADateExt: TDateExt; IsCurrentDate: Boolean; var Accept: Boolean);
  730. begin
  731. {Calculate 'My calcday'. Value of calcday is set in OnMonthChange
  732. event handler}
  733.      Accept := (ADateExt.DayNumber = CalcDay);
  734. end;
  735.  
  736. end.
  737.